package com.ohc.leetcode.栈;

import java.util.Stack;

/**
 * https://leetcode-cn.com/problems/implement-queue-using-stacks/
 * 2022年4月17日18:44:35
 */
public class 用栈实现队列232 {

    static class MyQueue {
        private Stack<Integer> InStack;
        private Stack<Integer> OutStack;

        public MyQueue() {
            this.InStack = new Stack<>();
            this.OutStack = new Stack<>();
        }

        public void push(int x) {
            InStack.push(x);
        }

        public int pop() {

            if (OutStack.isEmpty()) {
                while (!InStack.isEmpty()) {
                    OutStack.push(InStack.pop());
                }
                return OutStack.pop();
            } else {
                return OutStack.pop();

            }
        }

        public int peek() {
            if (OutStack.isEmpty()) {
                while (!InStack.isEmpty()) {
                    OutStack.push(InStack.pop());
                }
                return OutStack.peek();
            } else {
                return OutStack.peek();

            }
        }

        public boolean empty() {
            return OutStack.isEmpty();

        }
    }


    public static void main(String[] args) {
        用栈实现队列232 用栈实现队列232 = new 用栈实现队列232();
        MyQueue myQueue = new MyQueue();
        myQueue.push(1);
        System.out.println("myQueue.peek() = " + myQueue.peek());
        myQueue.push(2);
        System.out.println("myQueue.pop() = " + myQueue.pop());
        System.out.println("myQueue.pop() = " + myQueue.pop());

    }
}
